<?php
/*Created by	 : Israel Munoz
Created On		 : 15.08.2011
Modificated On	 : 25.08.2011
About			 : Suppliers and his orders administration pague.
*/

//include the functions.php
require "include/functions.php";
check_session();

//Check the user to be an administrator
check_admin();

//Define Data Variables
$Action='';
$InnerMenu='';
$message="";
$SupplierId=0;

//check if data exist and request it
if(!filter_has_var(INPUT_POST, "Action")){}
else
{
	$Action	= $_REQUEST["Action"];
}

if(!filter_has_var(INPUT_POST, "InnerMenu")){}
else
{
	$InnerMenu	= $_REQUEST["InnerMenu"];
}

if(!filter_has_var(INPUT_POST, "SupplierId")){}
else
{
	$SupplierId	= $_REQUEST["SupplierId"];
}

//PERFORM AN ACTION
switch($Action)
{
	case 'SaveSupplier':
		//Request Form Values
		
		$SupplierName	 = $_REQUEST["SupplierName"];
		$SupplierContact = $_REQUEST["SupplierContact"];
		$SupplierAddress = $_REQUEST["SupplierAddress"];
		$SupplierPhone	 = $_REQUEST["SupplierPhone"];
		$SupplierEmail	 = $_REQUEST["SupplierEmail"];
		
		
		//CALLS SAVE_SUPPLIER FUNCTIONS AND GET THE RESULT MESSAGE
		$message = save_supplier($SupplierName,$SupplierContact,$SupplierAddress,$SupplierPhone,$SupplierEmail,$SupplierId);
		$SupplierId=0;
		break;
		
	case 'DeleteSupplier':
		//CALLS DELETE_SUPPLIER FUNCTION AND GET THE RESULT MESSAGE
		$message = delete_supplier($SupplierId);
		$SupplierId=0;
		break;
		
	default:
	$message="";
}

//DISPLAY MESSAGE
if ($message!="")
{
	alert($message);
}

?>

<html>
<head>
<title>Online Computer Stores - Suppliers Administration</title>
<link rel="stylesheet" href="include/styles.css" type="text/css">
<script src="include/jquery-1.6.2.min.js"></script>
<script src="include/functions.js"></script>
<script>window.onload=function(){
	altRows('alternatecolor');
}</script>
</head>


<script>

//Control the Inner Menu
function ShowField(value)
{
		if (value=='ManageSuppliers') 
	{
			if (document.Data.InnerMenu.value!='')
			{
			//	 $('#ordersfield').hide('fast');
			}
		
			if (document.Data.InnerMenu.value!='ManageSuppliers')
			{		
				$('#suppsfield').show('slow');
				document.Data.InnerMenu.value='ManageSuppliers';
			}
	}

	
}

function SaveSupplier()
{
	//Check all the input box has data
	if ((document.Data.SupplierName.value != '') && (document.Data.SupplierContact.value != '') && (document.Data.SupplierAddress.value != '') && (document.Data.SupplierPhone.value != '') && (document.Data.SupplierEmail.value != ''))
	{
				//Submit the action
				document.Data.Action.value="SaveSupplier";	
				document.Data.submit();		

	}
	else
	{
		alert ('Please fill all the fields!');
		document.Data.SupplierName.focus();
	}
	
}

function DeleteSupplier(id)
{
	//Check the id
	if (id != 0)
	{
				//Submit the action
				document.Data.SupplierId.value=id;	
				document.Data.Action.value="DeleteSupplier";	
				document.Data.submit();		

	}
	else
	{
		alert ('Please select a record first!');
		document.Data.SupplierName.focus();
	}
	
}

function NewSupplier()
{
	document.Data.SupplierId.value=0;
	document.Data.SupplierName.value="";
	document.Data.SupplierContact.value="";
	document.Data.SupplierAddress.value="";
	document.Data.SupplierPhone.value="";
	document.Data.SupplierEmail.value="";
	
	document.Data.SupplierName.focus();
}


//This Function assign data by the ID  of a determinated field of a form
function AssignSuppId(data, formfield)
{
	data = data.split(';');
	
	document.Data.SupplierId.value=data[0];
	document.Data.SupplierName.value=data[1];
	document.Data.SupplierContact.value=data[2];
	document.Data.SupplierAddress.value=data[3];
	document.Data.SupplierPhone.value=data[4];
	document.Data.SupplierEmail.value=data[5];

	document.Data.SupplierName.focus();	
	
	//	eval ("document." + formfield + ".value = id");
}


</script>
<body>
<div align="center">
<div id="header">
		<div id="topmenu"><ul><?php top_menu(); ?></ul></div> 
            <div id="menu" align="right">
            <form name="form1" method="post" action="search.php">
                <tr>
                <td height="35" width="78"><a>Search Products</a></td>
                <td height="35" width="3">:</td>
                <td height="35" width="294"><input name="mysearch" type="text" id="mysearch">&nbsp;&nbsp;&nbsp;</td>
                </tr>
            </form>
            </div>
		<div id="menubar"> <!-- Menu buttons -->
            <ul>
            <li><a href="index.php" title="Home"><span>Home</span></a></li>
            <li><a href="#" title="Database Administration"><span>Database Maintenance</span></a></li>
            <li><a href="#" title="Stock Control"><span>Stock Levels</span></a></li>
            <li><a href="#" title="Ordering"><span>Ordering</span></a></li>
            </ul>
		</div>
 </div> <!-- Header -->
<div id="page">

	<form name="Data" method="post" action="suppliers_admin.php">	
	<input type="hidden" name="Action" value="<?=$Action?>" />
	<input type="hidden" name="InnerMenu" value="<?=$InnerMenu?>" />

		<h2>Suppliers Administration</h2> 
			
				<table  border="1" width="99%" height="65%">
					<tr>			
						<td width="15%">
						<h5>Please Select an Option</h5> 
							<input type="button" id="btnSupAdmin" class="newbutton"  value="Manage Suppliers" onClick="Javascript:ShowField('ManageSuppliers');">
							<br></br><p>
						   
							<br></br><p>
							<br></br><p>
							<br></br><p>
							<input type="button" id="btnBack" class="newbutton" value="Back" onClick="Javascript:window.location = 'admin_login.php';">
						</td>
						<td>
						<div id='suppsfield'>
						  <fieldset class="man_fieldset">
							<legend>Suppliers</legend> <br>
							<input type="hidden" name="SupplierId" value="<?=$SupplierId?>" />
							
							<table border="0"  width="100%">
								<tr>
									<td align="right">Name:</td>
									<td align="left"><input type="textbox" name="SupplierName" value=""></td>
								</tr>
								<tr> 
									<td align="right">Contact Name:</td>
									<td  align="left"><input type="textbox" name="SupplierContact" value=""></td>
								</tr>
								<tr>
									<td align="right">Address:</td>
									<td  align="left"><input type="textbox" name="SupplierAddress" value=""></td>
								</tr>
								<tr>                        
									<td align="right">Phone:</td>
									<td  align="left"><input type="textbox" name="SupplierPhone" value=""></td>

								</tr>
								   <tr>                        
									<td align="right">Email:</td>
									<td  align="left"><input type="textbox" name="SupplierEmail" value=""></td>

								</tr>
								<tr>
									<td colspan="2">&nbsp; </td>
								</tr>
								<tr>
									<td>&nbsp; </td>
									<td align="left">
											<input  align="right"	type="button" id="btnNewSup"  value="New" onClick="Javascript:NewSupplier();"> 
											<input  align="right"	type="button" id="btnSaveSup"  value="Save" onClick="Javascript:SaveSupplier();"> 
											<input  align="right" 	type="button" id="btnDelSup"   value="Delete" onClick="Javascript:DeleteSupplier(document.Data.SupplierId.value);"> 
									 </td>
								</tr>
							</table>
							</fieldset>
							
							<fieldset><legend>Existing Suppliers</legend>
							   <div class="man_list">
								<table class='altrowstable' id='alternatecolor'>
									<tr>
										<th><b>Id</b></th>
										<th><b>Name</b></th>
										<th><b>Contact</b></th>
										<th><b>Address</b></th>
										<th><b>Phone</b></th>                                
										<th><b>Email</b></th>
										<th><b>Action</b></th>                                 
									</tr>
									<?php 
										//CALL SHOW_SUPPLIERS PHP FUNCTION AND GET THE DATA
										print_r (show_suppliers());				
									?>
								</table>
							   </div> 
						    </fieldset>
					    </div>
					    </td>			
					</tr>					
				</table>
		<br>
		<br>
		<?php
		/*
			print('<a>Details have been retrieved from database</a><br>
			<a>Username: '.$_SESSION['name'].'</a><br> 
			<a>Password: xxxxxxxx</a><br>  
			<a>User Level: '.$_SESSION['valid_id'].'</a><br>
			<a>Session Time: '.$_SESSION['valid_time'].'</a><br>
			'); // Retrieves and displayers the data from column "rights" you could use this to retreive cart or profile info
		*/
		?>
	</form>
</div> <!-- end of page DIV -->

<? footer(); ?>
</body>
  <script>
if (document.Data.InnerMenu.value =="")
{
	$('#suppsfield').hide(1);	
}
else{
	ShowField(document.Data.InnerMenu.value);
}
</script>
</html>